package com.leadnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leadnews.pojo.Order;
import com.leadnews.pojo.User;
import com.leadnews.service.OrderService;
import com.leadnews.mapper.OrderMapper;
import com.leadnews.utils.TokenFilter;
import com.leadnews.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author 自己
* @description 针对表【order】的数据库操作Service实现
* @createDate 2023-02-19 19:25:40
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{
@Autowired
    TokenFilter tokenFilter;
@Autowired
OrderMapper orderMapper;
    @Override
    public ResponseResult publish(Order order, HttpServletRequest request) {
        ResponseResult result = new ResponseResult();
        User user = tokenFilter.getToken(request);
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        order.setCreateTime(format);
        order.setUserId(user.getId()+"");
        order.setStatus(0+"");
//        int i=orderMapper.inserByorder(order.getUserId(),order.getClassfiy1(),order.getContent(),order.getCreateTime(),
//                order.getMoney1(),order.getPhone(),order.getQq1(),order.getStatus(),order.getTime1(),order.getOrderType(),order.getTitle1());
        System.out.println(order.toString());
        int i = orderMapper.insert(order);
        if (i>0){
            result.setCode(200);
            result.setErrorMessage("订单待审核，请等待");
        }
        return result;
    }

    @Override
    public ResponseResult selectOrder(Map<String, String> map, HttpServletRequest request) {
        User user = tokenFilter.getToken(request);
        ResponseResult result = new ResponseResult();
        String index = map.get("index");
        LambdaQueryWrapper<Order> queryWrapper = new LambdaQueryWrapper<>();
        if (index.equals("我发布的")||index.equals("全部")){
            queryWrapper.eq(Order::getUserId,user.getId());
            List<Order> list= orderMapper.selectList(queryWrapper);
            result.setCode(200);
            result.setData(list);
            return result;
        }
        return null;
    }

    @Override
    public ResponseResult deleteOrder(Order order, HttpServletRequest request) {
        User user = tokenFilter.getToken(request);
        ResponseResult result = new ResponseResult();
        if (!order.getUserId().equals(user.getId()+"")){
            result.setCode(0);
            result.setErrorMessage("无法删除");
            return result;
        }
        int i = orderMapper.deleteById(order.getId());
        if (i>0){
            result.setCode(200);
            result.setErrorMessage("删除成功");
        }
        return result;
    }
}




